package luke.ui.components;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;

public class AnimatedProgressBar extends JProgressBar {
    private Timer animationTimer;
    private int pulsePosition = -30;
    private int animationSpeed = 30;
    private boolean isAnimating = false;

    public AnimatedProgressBar() {
        super();
        setupAnimation();
    }

    public AnimatedProgressBar(int orient) {
        super(orient);
        setupAnimation();
    }

    public AnimatedProgressBar(int min, int max) {
        super(min, max);
        setupAnimation();
    }

    public AnimatedProgressBar(int orient, int min, int max) {
        super(orient, min, max);
        setupAnimation();
    }

    private void setupAnimation() {
        animationTimer = new Timer(animationSpeed, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isAnimating) {
                    updatePulsePosition();
                    repaint();
                }
            }
        });
        animationTimer.setRepeats(true);
    }

    private void updatePulsePosition() {
        pulsePosition += 5;
        if (pulsePosition > getWidth() + 30) {
            pulsePosition = -30;
        }
    }

    public void setDonationStatus(boolean isDonated) {
        animationSpeed = isDonated ? 10 : 120;
        if (animationTimer != null) {
            animationTimer.setDelay(animationSpeed);
        }
    }

    public void startAnimation() {
        if (!isAnimating && animationTimer != null) {
            isAnimating = true;
            animationTimer.start();
            repaint();
        }
    }

    public void stopAnimation() {
        if (isAnimating && animationTimer != null) {
            isAnimating = false;
            animationTimer.stop();
            pulsePosition = -30;
            repaint();
        }
    }

    public void resetAnimation() {
        stopAnimation();
        pulsePosition = -30;
    }

    public boolean isAnimating() {
        return isAnimating;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (isAnimating && isVisible() && getWidth() > 0 && getHeight() > 0) {
            Graphics2D g2d = (Graphics2D) g.create();
            try {
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 计算已完成进度宽度
                int progressWidth = 0;
                if (getMaximum() != getMinimum()) {
                    progressWidth = (int) ((getValue() - getMinimum())
                            / (double) (getMaximum() - getMinimum()) * getWidth());
                }

                // 关键修复：进度为0时不绘制动画
                if (progressWidth <= 0) {
                    return;
                }

                // 设置剪切区域，限制动画在已完成进度内
                g2d.setClip(0, 0, progressWidth, getHeight());

                // 绘制渐变脉冲
                int pulseWidth = 30;
                Point2D start = new Point2D.Float(pulsePosition - pulseWidth, 0);
                Point2D end = new Point2D.Float(pulsePosition + pulseWidth, 0);
                float[] fractions = {0.0f, 0.5f, 1.0f};
                Color[] colors = {
                        new Color(255, 255, 255, 0),
                        new Color(255, 255, 255, 120),
                        new Color(255, 255, 255, 0)
                };
                LinearGradientPaint gradient = new LinearGradientPaint(start, end, fractions, colors);

                g2d.setPaint(gradient);
                g2d.fillRect(pulsePosition - pulseWidth, 0, pulseWidth * 2, getHeight());
            } finally {
                g2d.dispose();
            }
        }
    }

    @Override
    public void setValue(int n) {
        super.setValue(n);
    }

    @Override
    public void setIndeterminate(boolean newValue) {
        super.setIndeterminate(newValue);
    }
}
